package com.iss.demo.dao;

import com.iss.demo.entity.Car;
import com.iss.demo.model.CarModel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;

@Mapper
@Repository
public interface CarDao {

    @Select("select * from car INNER JOIN brand  INNER JOIN drive INNER JOIN inflow INNER JOIN type\n" +
            "on c_brand_id=b_id and c_drive_id=d_id and c_inflow_id=i_id and c_type_id=t_id")
    public List<CarModel> selectAll();

    @Select("select * from car INNER JOIN brand  INNER JOIN drive INNER JOIN inflow INNER JOIN type\n" +
            "on c_brand_id=b_id and c_drive_id=d_id and c_inflow_id=i_id and c_type_id=t_id where b_name=#{b_name}")
    public List<CarModel> selectAllByBrand(String b_name);

    @Select("SELECT * from inventory INNER JOIN car on in_car_id=c_id")
    public List<CarModel> selectInventory();

    @Select("select * from car INNER JOIN brand  INNER JOIN drive INNER JOIN inflow INNER JOIN type INNER JOIN inventory\n" +
            "on c_brand_id=b_id and c_drive_id=d_id and c_inflow_id=i_id and c_type_id=t_id and in_car_id=c_id where t_name=#{t_name};")
    public List<CarModel> selectAllByType(String t_name);

    @Select("select * from car INNER JOIN brand  INNER JOIN drive INNER JOIN inflow INNER JOIN type INNER JOIN inventory\n" +
            "on c_brand_id=b_id and c_drive_id=d_id and c_inflow_id=i_id and c_type_id=t_id and in_car_id=c_id where c_name=#{c_name}")
    public CarModel selectAllByName(String c_name);

    @Select("select * from car INNER JOIN brand  INNER JOIN drive INNER JOIN inflow INNER JOIN type INNER JOIN inventory\n"  +
            "on c_brand_id=b_id and c_drive_id=d_id and c_inflow_id=i_id and c_type_id=t_id and in_car_id=c_id where c_id=#{c_id}")
    public CarModel selectAllById(Integer c_id);

    @Select("<script>select * from car INNER JOIN brand  INNER JOIN drive INNER JOIN inflow INNER JOIN type INNER JOIN inventory\n" +
            "on c_brand_id=b_id and c_drive_id=d_id and c_inflow_id=i_id and c_type_id=t_id and in_car_id=c_id" +
            "<where>" +
            "<if test='t_name!=null'>and t_name like #{t_name}</if>" +
            "<if test='b_name!=null'>and b_name like #{b_name}</if>" +
            "<if test='c_min_price!=null'>and c_price BETWEEN #{c_min_price} and #{c_max_price}</if>" +
            "</where></script>")
    public List<CarModel> selectAllByOther(@Param("t_name") String t_name,@Param("b_name") String b_name,@Param("c_min_price") Integer c_min_price,@Param("c_max_price") Integer c_max_price);

    @Insert("insert into car (c_name,c_displacement,c_gearbox,c_structure,c_braking,c_maxpower,c_maxtorque," +
            "c_maxhorsepower,c_brand_id,c_drive_id,c_inflow_id,c_type_id) value(#{c_name},#{c_displacement}," +
            "#{c_gearbox},#{c_structure},#{c_braking},#{c_maxpower},#{c_maxtorque},#{c_maxhorsepower},#{c_brand_id}," +
            "#{c_drive_id},#{c_inflow_id},#{c_type_id})")
    public Integer insertCar(Car car);



    //添加库存
    @Insert("INSERT into inventory (c_price,c_color,c_count,in_car_id) VALUES(#{c_price},#{c_color},#{c_count},#{c_id});")
    public Integer insertInventory(Car car);


    @Update("update car set c_name=#{c_name},c_displacement=#{c_displacement},c_gearbox=#{c_gearbox}," +
            "c_structure=#{c_structure},c_braking=#{c_braking},c_maxpower=#{c_maxpower},c_maxtorque=#{c_maxtorque}," +
            "c_maxhorsepower=#{c_maxhorsepower},c_brand_id=#{c_brand_id},c_drive_id=#{c_drive_id}," +
            "c_inflow_id=#{c_inflow_id},c_type_id=#{c_type_id} where c_id=#{c_id}")
    public Integer updateCar(Car car);


    @Select("select * from car where c_id=#{id}")
    public Car selectById(Integer id);

    @Update("update car set c_surface = #{surface} where c_id=#{id}")
    public Integer updateInfo(@Param("surface") String surface, @Param("id") Integer id);

    @Delete(" <script>"+"delete from car where c_id in" +
            "    <foreach collection=\"array\" item=\"id\" separator=\",\" open=\"(\" close=\")\">" +
            "        #{id}" +
            "    </foreach></script>")
    public Integer delete(Integer[] c_ids);

    @Delete("delete from inventory where in_id=#{in_id}")
    public Integer deleteInventory(Integer in_id);




}
